#include<iostream>
using namespace std;

int a[101][101];

int main()
{
	int m, n;
	cin>>m>>n;
	for(int i = 1;i <= m;i++)
		for(int j = 1;j <= n;j++)
			cin>>a[i][j];
	
	int dx , dy, cnt = 0;
	int i = 1, j = 0;
	while(cnt < m*n)
	{
		dx = 0, dy = 1;
		while(a[i][j+dy])
		{
			cnt++;
			j = j+dy;
			cout<<a[i][j]<<endl;
			a[i][j] = 0;
		}
		
		dx = 1, dy = 0;
		while(a[i+dx][j])
		{
			cnt++;
			i = i+dx;
			cout<<a[i][j]<<endl;
			a[i][j] = 0;
		}
		
		dx = 0, dy = -1;
		while(a[i][j+dy])
		{
			cnt++;
			j = j+dy;
			cout<<a[i][j]<<endl;
			a[i][j] = 0;
		}
		
		dx = -1, dy = 0;
		while(a[i+dx][j])
		{
			cnt++;
			i = i+dx;
			cout<<a[i][j]<<endl;
			a[i][j] = 0;
		}
	}
	return 0;
}
